home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #1 / Amiga Plus CD - 2000 - No. 1.iso / Tools / Text / Edit / GoldED-Demo / installdata / golded / developer / api / examples / x / funcs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-12-03  |  7.0 KB  |  256 lines

  1. /* -----------------------------------------------------------------------------
  2.  
  3.  x.api ©1999 Dietmar Eilert
  4.  
  5.  Plug-in example. This plug-in opens a container and draws a "X". Dice:
  6.  
  7.  DMAKE
  8.  
  9.  -------------------------------------------------------------------------------
  10.  
  11. */
  12.  
  13. #include "defs.h"
  14.  
  15. /// "prototypes"
  16.  
  17. // library functions
  18.  
  19. Prototype LibCall struct APIClient *APIMountClient(__A0 struct APIMessage *, __A1 char *);
  20. Prototype LibCall void              APICloseClient(__A0 struct APIClient  *, __A1 struct APIMessage *);
  21. Prototype LibCall void              APIBriefClient(__A0 struct APIClient  *, __A1 struct APIMessage *);
  22. Prototype LibCall void              APIFree       (__A0 struct APIClient  *, __A1 struct APIOrder   *);
  23.  
  24. // private functions
  25.  
  26. Prototype ULONG DispatchContainer(struct APIClient *, struct APIMessage *);
  27.  
  28. ///
  29. /// "library functions"
  30.  
  31. LibCall struct APIClient *
  32. APIMountClient(__A0 struct APIMessage *apiMsg, __A1 char *args)
  33. {
  34.     struct APIClient *apiClient;
  35.  
  36.     // fill out a description of this client
  37.  
  38.     if (apiClient = (struct APIClient *)AllocVec(sizeof(struct APIClient), MEMF_ANY | MEMF_CLEAR)) {
  39.  
  40.         if (apiClient->api_Area = (struct APIArea *)AllocVec(sizeof(struct APIArea), MEMF_ANY | MEMF_CLEAR)) {
  41.  
  42.             // client description
  43.  
  44.             apiClient->api_APIVersion = API_INTERFACE_VERSION;
  45.             apiClient->api_Version    = 1;
  46.             apiClient->api_Name       = "X";
  47.             apiClient->api_Info       = "Plug-in example";
  48.             apiClient->api_Commands   = NULL;
  49.             apiClient->api_Serial     = 0;
  50.             apiClient->api_Classes    = API_CLASS_SYSTEM | API_CLASS_CONTAINER;
  51.  
  52.             // prepare the container request
  53.  
  54.             apiClient->api_Area->api_Width     = 20;
  55.             apiClient->api_Area->api_Height    = 5;
  56.             apiClient->api_Area->api_UnitsX    = API_UNITS_FONT;
  57.             apiClient->api_Area->api_UnitsY    = API_UNITS_FONT;
  58.             apiClient->api_Area->api_Alignment = API_ALIGN_LEFT;
  59.             apiClient->api_Area->api_Style     = API_STYLE_STANDARD;
  60.             apiClient->api_Area->api_IDCMP     = 0;
  61.         }
  62.         else {
  63.  
  64.             FreeVec(apiClient);
  65.  
  66.             apiClient = NULL;
  67.         }
  68.     }
  69.  
  70.     return(apiClient);
  71. }
  72.  
  73. LibCall void
  74. APICloseClient(__A0 struct APIClient *handle, __A1 struct APIMessage *apiMsg)
  75. {
  76.     // free ressources related to this client
  77.  
  78.     FreeVec(handle->api_Area);
  79.  
  80.     FreeVec(handle);
  81. }
  82.  
  83. LibCall void
  84. APIBriefClient(__A0 struct APIClient *handle, __A1 struct APIMessage *apiMsg)
  85. {
  86.     struct APIMessage *msg;
  87.  
  88.     // handle host's command notify
  89.  
  90.     for (msg = apiMsg; msg; msg = msg->api_Next) {
  91.  
  92.         if (msg->api_State == API_STATE_NOTIFY) {
  93.  
  94.             switch (msg->api_Class) {
  95.  
  96.                 case API_CLASS_CONTAINER:
  97.  
  98.                     msg->api_Error = DispatchContainer(handle, msg);
  99.  
  100.                     break;
  101.  
  102.                 case API_CLASS_SYSTEM:
  103.  
  104.                     msg->api_Error = API_ERROR_OK;
  105.  
  106.                     break;
  107.  
  108.                 default:
  109.  
  110.                     msg->api_Error = API_ERROR_UNKNOWN;
  111.             }
  112.         }
  113.     }
  114. }
  115.  
  116.  
  117. LibCall void
  118. APIFree(__A0 struct APIClient *handle, __A1 struct APIOrder *apiOrder)
  119. {
  120.     // no order-related ressources to be freed (this example doesn't send orders to the host)
  121. }
  122.  
  123. ///
  124. /// "private functions"
  125.  
  126. /* ----------------------------- DispatchContainer -----------------------------
  127.  
  128.  Handle container-related event
  129.  
  130. */
  131.  
  132. ULONG
  133. DispatchContainer(handle, apiMsg)
  134.  
  135. struct APIClient  *handle;
  136. struct APIMessage *apiMsg;
  137. {
  138.     ULONG error = API_ERROR_OK;
  139.  
  140.     switch (apiMsg->api_Action) {
  141.  
  142.         case API_ACTION_DETACH:
  143.  
  144.             // request to detach gadgets and stop asynchronous rendering because user is about to resize the window or the container (this example doesn't use gadgets)
  145.  
  146.             break;
  147.  
  148.         case API_ACTION_ATTACH:
  149.  
  150.             // request to attach gadgets after the container or window has been resized (this example doesn't use gadgets); avoid rendering at this point (API_ACTION_REFRESH will follow soon)
  151.  
  152.             break;
  153.  
  154.         case API_ACTION_REFRESH:
  155.  
  156.             // refresh the user interface: fall through to initial display refresh function
  157.  
  158.         case API_ACTION_PAINT:
  159.  
  160.             // we should render our display; this example simply draws a big "X"
  161.  
  162.             {
  163.                 struct APIInstance  *instance;
  164.                 struct APIContainer *container;
  165.  
  166.                 instance = apiMsg->api_Instance;
  167.  
  168.                 if (container = instance->api_Container) {
  169.  
  170.                     // container not fully obscured ?
  171.  
  172.                     if (container->api_Clipping) {
  173.  
  174.                         struct Region   *oldregion;
  175.                         struct RastPort *rast;
  176.                         UWORD            x0;
  177.                         UWORD            y0;
  178.                         UWORD            x1;
  179.                         UWORD            y1;
  180.                         BOOL             clipping;
  181.  
  182.                         oldregion = (struct Region *)~0;
  183.  
  184.                         // clipping required (rendering area smaller than container) ?
  185.  
  186.                         clipping = ((container->api_Clipping->MaxX != container->api_Dimensions->MaxX) || (container->api_Clipping->MaxY != container->api_Dimensions->MaxY));
  187.  
  188.                         // install clipping rectangle
  189.  
  190.                         if (clipping) {
  191.  
  192.                             struct Region *region;
  193.  
  194.                             if (region = NewRegion()) {
  195.  
  196.                                 if (OrRectRegion(region, container->api_Clipping)) {
  197.  
  198.                                     oldregion = InstallClipRegion(instance->api_Window->WLayer, region);
  199.                                 }
  200.                                 else
  201.                                     DisposeRegion(region);
  202.                             }
  203.                         }
  204.  
  205.                         // rendering
  206.  
  207.                         x0 = container->api_Dimensions->MinX;
  208.                         y0 = container->api_Dimensions->MinY;
  209.                         x1 = container->api_Dimensions->MaxX;
  210.                         y1 = container->api_Dimensions->MaxY;
  211.  
  212.                         rast = container->api_RPort;
  213.  
  214.                         SetAPen(rast, container->api_DrawInfo->dri_Pens[SHADOWPEN]);
  215.  
  216.                         Move(rast, x0, y0);
  217.  
  218.                         Draw(rast, x1, y1);
  219.  
  220.                         Move(rast, x1, y0);
  221.  
  222.                         Draw(rast, x0, y1);
  223.  
  224.                         // remove clipping
  225.  
  226.                         if (oldregion != (struct Region *)~0) {
  227.  
  228.                             struct Region *region;
  229.  
  230.                             if (region = InstallClipRegion(instance->api_Window->WLayer, oldregion))
  231.  
  232.                                 DisposeRegion(region);
  233.                         }
  234.  
  235.                     }
  236.                 }
  237.             }
  238.  
  239.             break;
  240.  
  241.         case API_ACTION_INPUTEVENT:
  242.  
  243.             // input event to be handled
  244.  
  245.             break;
  246.  
  247.         default:
  248.  
  249.             error = API_ERROR_UNKNOWN;
  250.     }
  251.  
  252.     return(error);
  253. }
  254.  
  255. ///
  256.